package com.hxf.web.filter;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Properties;
 
import javax.annotation.Resource;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.hxf.common.AppProps;
import com.hxf.common.GlobalConstant;  
import com.hxf.dto.AdminUserDto;
import com.hxf.dto.RestaurentUserDto;
import com.hxf.dto.UserDto;
import com.hxf.exception.ServiceException;
import com.hxf.service.user.AdminUserService;
import com.hxf.service.user.RestaurantUserService;
import com.hxf.service.user.UserService;

 /**
  * 用户登录过滤
  * @author 滕易成
  *
  */
public class UserLoginFilter implements Filter {
	private static final String LOGIN_PAGE =  "/"+GlobalConstant.PROJECT_NAME+"/login.jsp";
	private static final String LOGOUT_PAGE =  "/"+GlobalConstant.PROJECT_NAME+"/user/login!userFilterMsg.html?msg="; 
	
	@Override
	public void destroy() {  
	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain chain) throws IOException, ServletException { 
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		Object adminUserDto = request.getSession().getAttribute("adminUserDto"); //联盟管理员Session
		Object restaurentUserDto = request.getSession().getAttribute("restaurentUserDtoLogin");//餐厅 Session 
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		AppProps.load();
		String whileUrl = AppProps.getProperty("while.url");
		//过滤白名单
		if (whileUrl.indexOf(request.getServletPath()) > -1) {
			chain.doFilter(servletRequest, servletResponse); 
			return;
			
		} else if (adminUserDto != null) { 
			WebApplicationContext wac = WebApplicationContextUtils .getRequiredWebApplicationContext(request.getSession().getServletContext());
			AdminUserService userService = (AdminUserService)wac.getBean("adminUserService");
			AdminUserDto ud = (AdminUserDto)adminUserDto;
			AdminUserDto uDto = null; 
			try { 
				uDto = (AdminUserDto)userService.get(AdminUserDto.class, ud.getId());
				//审核不通过
				if (uDto.getStatus() == UserDto.USER_STATUS_2) {
					response.sendRedirect(LOGOUT_PAGE+1); 
					request.getSession().setAttribute("adminUserDto", null);
					request.getSession().removeAttribute("adminUserDto");
					return;
				}
				//锁定
				if (uDto.getStatus() == UserDto.USER_STATUS_3) {
					response.sendRedirect(LOGOUT_PAGE+2); 
					request.getSession().setAttribute("adminUserDto", null);
					request.getSession().removeAttribute("adminUserDto");
					return;
					
				}
				//作废
				if (uDto.getStatus() == UserDto.USER_STATUS_4) {
					response.sendRedirect(LOGOUT_PAGE + 3); 
					request.getSession().setAttribute("adminUserDto", null);
					request.getSession().removeAttribute("adminUserDto");
					return;
					
				}
				
				//验证是否在其他地方登录 用版本号比较
				/*if (ud.getLoginVersion() != null && uDto != null && !ud.getLoginVersion().equals(uDto.getLoginVersion())) {
					response.sendRedirect(LOGOUT_PAGE+4); 
					request.getSession().setAttribute("userDto", null);
					request.getSession().removeAttribute("userDto");
					return;
				}*/
				chain.doFilter(servletRequest, servletResponse); 
				return;
			} catch (ServiceException e) {
				 e.printStackTrace();
			}
		} else if (null != restaurentUserDto) {//餐厅
			WebApplicationContext wac = WebApplicationContextUtils .getRequiredWebApplicationContext(request.getSession().getServletContext());
			RestaurantUserService userService = (RestaurantUserService)wac.getBean("restaurantUserService");
			RestaurentUserDto ud = (RestaurentUserDto)restaurentUserDto;//餐厅实体类
			RestaurentUserDto uDto = null; 
			try { 
				//餐厅   UserDto
				uDto = (RestaurentUserDto)userService.get(RestaurentUserDto.class, ud.getId());
				//审核不通过
				if (uDto.getStatus() == UserDto.USER_STATUS_2) {
					response.sendRedirect(LOGOUT_PAGE+1); 
					request.getSession().setAttribute("restaurentUserDtoLogin", null);
					request.getSession().removeAttribute("restaurentUserDtoLogin");
					return;
				}
				//锁定
				if (uDto.getStatus() == UserDto.USER_STATUS_3) {
					response.sendRedirect(LOGOUT_PAGE+2); 
					request.getSession().setAttribute("restaurentUserDtoLogin", null);
					request.getSession().removeAttribute("restaurentUserDtoLogin");
					return;
					
				}
				//作废
				if (uDto.getStatus() == UserDto.USER_STATUS_4) {
					response.sendRedirect(LOGOUT_PAGE + 3); 
					request.getSession().setAttribute("restaurentUserDtoLogin", null);
					request.getSession().removeAttribute("restaurentUserDtoLogin");
					return;
					
				}
				
				//验证是否在其他地方登录 用版本号比较
				/*if (ud.getLoginVersion() != null && uDto != null && !ud.getLoginVersion().equals(uDto.getLoginVersion())) {
					response.sendRedirect(LOGOUT_PAGE+4); 
					request.getSession().setAttribute("userDto", null);
					request.getSession().removeAttribute("userDto");
					return;
				}*/
				chain.doFilter(servletRequest, servletResponse); 
				return;
			} catch (ServiceException e) {
				 e.printStackTrace();
			}
			
		}else {
			//登陆页面
			response.sendRedirect(LOGIN_PAGE);
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException { 
		
	} 
}
